package com.app.milkteaapp.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.app.milkteaapp.bean.LoginGsonBean;
import com.app.milkteaapp.bean.UpdateGsonBean;
import com.app.milkteaapp.R;
import com.app.milkteaapp.utils.GsonUtils;
import com.app.milkteaapp.utils.MyTools;
import com.app.milkteaapp.utils.OkHttpUtils;
import com.app.milkteaapp.utils.SpUtils;

import org.jetbrains.annotations.NotNull;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UpdateUserActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText et_email;
    private EditText et_password;
    private EditText et_password_1;
    private Spinner sp_gender;
    private EditText et_phone;
    private Button btn_confirm;
    private String email;
    private String password;
    private String phone;
    private String password_1;
    Context context = this;
    private LoginGsonBean.User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update_user);
        initView();
    }

    private void initView() {
        et_email = (EditText) findViewById(R.id.et_email_update);
        et_password = (EditText) findViewById(R.id.et_password_update);
        et_password_1 = (EditText) findViewById(R.id.et_password_1_update);
        sp_gender = (Spinner) findViewById(R.id.sp_gender_update);
        et_phone = (EditText) findViewById(R.id.et_phone_update);
        btn_confirm = (Button) findViewById(R.id.btn_confirm_update);
        user = SpUtils.getUserBean(context);

        btn_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_confirm_update:
                if (submit()) {
                    if(password.equals(password_1)){
                        //json数据封装
                        JSONObject jsonObject = new JSONObject();
                        try {
                            jsonObject.put("id",SpUtils.getUserBean(context).getId());
                            jsonObject.put("email",email);
                            jsonObject.put("password",password);
                            jsonObject.put("sex",MyTools.SexToint(sp_gender.getSelectedItem().toString()));
                            jsonObject.put("phone",phone);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        Log.e("_____________", "啊"+jsonObject.toString() );
                        OkHttpUtils.MyCall(MyTools.URL+"/user/update",jsonObject.toString()).enqueue(new Callback() {
                            @Override
                            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(context, "网络炸了", Toast.LENGTH_SHORT).show();
                                    }
                                });
                            }

                            @Override
                            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                //gson解析
                                String string = response.body().string();
                                //如果请求成功,则返回登录页面
                                UpdateGsonBean updateGsonBean = GsonUtils.fromJson(string,UpdateGsonBean.class);

                                if(updateGsonBean.isSuccess()){
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(context, "请重新登录!", Toast.LENGTH_SHORT).show();
                                            Intent intent = new Intent(UpdateUserActivity.this,LoginActivity.class);
                                            startActivity(intent);
                                            finish();
                                        }
                                    });


                                }

                            }
                        });


                    }else{
                        Toast.makeText(this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
        }
    }

    private boolean submit() {
        email = et_email.getText().toString().trim();
        password = et_password.getText().toString().trim();
        phone = et_phone.getText().toString().trim();
        password_1 = et_password_1.getText().toString().trim();
        if (TextUtils.isEmpty(email)) {
            email = user.getEmail();
        } else if (TextUtils.isEmpty(password)) {
            password = user.getPassword();
        } else if (!TextUtils.isEmpty(phone)) {
            if (TextUtils.isEmpty(password_1)) {
                Toast.makeText(context, "请再次输入新密码！", Toast.LENGTH_SHORT).show();
            }
        } else if (TextUtils.isEmpty(phone)) {
            phone = user.getPhone();
        }
        return true;
    }
}
